package com.doit.demo04;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class test_Web {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true){
            Socket accept = serverSocket.accept();
            new Thread(()->{
                try {
                    InputStream ins = accept.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(ins));
                    String path = br.readLine().split(" +")[1].substring(1);

                    OutputStream out = accept.getOutputStream();
                    //三行固定代码
                    out.write("HTTP/1.1 200 OK\r\n".getBytes());
                    out.write("Content-Type:text/html\r\n".getBytes());
                    out.write("\r\n".getBytes());

                    FileInputStream in = new FileInputStream(path);
                    byte[] bytes = new byte[1024];
                    int len;
                    while ((len=in.read(bytes))!=-1){
                        out.write(bytes,0,len);
                    }
                    accept.shutdownOutput();
                    out.close();
                    in.close();
                    br.close();
                }catch (IOException e)
                {
                    System.out.println("窝嫩跌");
                }
            }).start();
        }
    }
}
